package com.zh.web.controller;

import com.github.AopLog;
import com.zh.business.service.web.common.TokenService;
import com.zh.zhbootcommon.config.minio.MinIoProperties;
import com.zh.zhbootcommon.domain.DResult;
import com.zh.zhbootcommon.utils.io.MinIoUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 描述: 公共数值获取
 *
 * @author: yanglin
 * @Date: 2021-08-31-9:34
 * @Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/mp/common/v1")
@Api(value = "公共数值相关")
@AopLog(type = "公共数值相关", stackTraceOnErr = true)
public class CommonController {

    @Autowired
    TokenService tokenService;

    @Autowired
    MinIoProperties minIoProperties;

    @Autowired
    MinIoUtils minIoUtil;

    @ApiOperation(value = "获取Token接口")
    @GetMapping("/getToken")
    public DResult getWechatToken() {
        String token = tokenService.createToken();
        return DResult.of(token);
    }

    @ApiOperation(value = "获取Token接口(Params)")
    @GetMapping("/getTokenByParams")
    public DResult getTokenByParams(HttpServletRequest request) {
        String token = tokenService.createToken(request);
        return DResult.of(token);
    }

    @ApiOperation(value = "MinIo上传文件")
    @PostMapping(value = "/upload")
    public DResult upload(@RequestParam("file") MultipartFile file) {
        String fileUrl = minIoUtil.upload(minIoProperties.getBucketName(), file);
        return DResult.of(fileUrl);
    }

    @ApiOperation(value = "MinIo下载文件")
    @GetMapping(value = "/download")
    public void download(@RequestParam("fileName") String fileName, HttpServletResponse response) {
        minIoUtil.download(minIoProperties.getBucketName(), fileName, response);
    }

    @ApiOperation(value = "MinIo删除文件")
    @GetMapping(value = "/delete")
    public DResult delete(@RequestParam("fileName") String fileName) {
        minIoUtil.deleteFile(minIoProperties.getBucketName(), fileName);
        return DResult.success();
    }
}
